package parser

import (
	"gxgeek.com/gxgeek/learngo/crawler/engine"
	"regexp"
	"strconv"
	"gxgeek.com/gxgeek/learngo/crawler/model"
	"gxgeek.com/gxgeek/learngo/crawler/config"
)

var ageRe = regexp.MustCompile(`<td><span class="label">年龄：</span>([\d]+)岁</td>`)
var marriageRe = regexp.MustCompile(`<td><span class="label">婚况：</span>([^<]+)</td>`)
var incomeRe = regexp.MustCompile(`<td><span class="label">月收入：</span>([^<]+)</td>`)
var hokouRe = regexp.MustCompile(`<td><span class="label">籍贯：</span>([^<]+)</td>`)
var xinzuoRe = regexp.MustCompile(`<td><span class="label">星座：</span>([^<]+)</td>`)
var carRe = regexp.MustCompile(`<td><span class="label">是否购车：</span><span field="">([^<]+)</span></td>`)
var HouseRe = regexp.MustCompile(`<td><span class="label">住房条件：</span><span field="">([^<]+)</span></td>`)
var weightRe = regexp.MustCompile(`<td><span class="label">体重：</span><span field="">([\d]+)KG</span></td>`)
var heightRe = regexp.MustCompile(`<td><span class="label">身高：</span>([\d]+)CM</td>`)
var educationRe = regexp.MustCompile(`<td><span class="label">学历：</span>([^<]+)</td>`)
var generRe = regexp.MustCompile(`<td><span class="label">性别：</span><span field="">([^<]+)</span></td>`)
var occupationRe = regexp.MustCompile(`<td><span class="label">职业：</span><span field="">([^<]+)</span></td>`)

var guessRs = regexp.MustCompile(`<a class="exp-user-name"[^>]*href="(http://album.zhenai.com/u/[\d]+)">([^<]+)`)
var idUrlRe = regexp.MustCompile(`http://album.zhenai.com/u/([\d]+)`)

func parseProfile(bytes []byte, url string, name string) engine.ParseResult {
	profile := model.Profile{}
	profile.Name = name
	age, err := strconv.Atoi(extractString(bytes, ageRe))
	if err != nil {
		//panic(err)
	}
	profile.Age = age
	profile.Marriage = extractString(bytes, marriageRe)
	profile.Xinzuo = extractString(bytes, xinzuoRe)
	profile.Car = extractString(bytes, carRe)
	profile.House = extractString(bytes, HouseRe)
	profile.Hokou = extractString(bytes, hokouRe)
	weight, err := strconv.Atoi(extractString(bytes, weightRe))
	if err != nil {
		//panic(err)
	}
	profile.Weight = weight
	height, err := strconv.Atoi(extractString(bytes, heightRe))
	if err != nil {
		//panic(err)
	}
	profile.Height = height
	profile.Income = extractString(bytes, incomeRe)
	profile.Education = extractString(bytes, educationRe)
	profile.Gender = extractString(bytes, generRe)
	profile.Occupation = extractString(bytes, occupationRe)

	result := engine.ParseResult{
		Items: []engine.Item{
			{
				Url:     url,
				Type:    "zhenai",
				Id:      extractString([]byte(url), idUrlRe),
				Payload: profile,
			},
		},
	}

	match := guessRs.FindAllSubmatch(bytes, -1)
	for _, s := range match {
		result.Requests = append(result.Requests,
			engine.Request{
				Url: string(s[1]),
				Parser: NewProfileParser(string(s[2])),
			})

	}

	return result
}

func extractString(contents []byte, re *regexp.Regexp) string {
	match := re.FindSubmatch(contents)

	if len(match) >= 2 {
		return string(match[1])
	}
	return ""

}
type ProfileParser struct {
	userName string
}

func (p * ProfileParser) Parse(contents [] byte, url string) engine.ParseResult {
	return parseProfile(contents, url, p.userName)
}

func (p * ProfileParser) Serialize() (name string, args interface{}) {
	return config.ParseProfile,p.userName
}
func NewProfileParser(userName string) *ProfileParser {
	return &ProfileParser{userName: userName}
}

